package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.Cart;
import com.zhentao.pojo.Product;
import com.zhentao.service.CartService;
import com.zhentao.service.ProductService;
import com.zhentao.service.UserService;
import com.zhentao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    ProductService productService;
    @Autowired
    UserService userService;
    @RequestMapping("/findByUserId")
    public Result add(Integer id){
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<Cart> carts=cartService.list(queryWrapper);
        for (Cart cart:carts){
            cart.setUser(userService.getById(cart.getUserId()));
            cart.setProduct(productService.getById(cart.getProductId()));
        }
        return carts!=null?Result.OK(carts):Result.ERROR();
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Cart cart){
        boolean save = cartService.save(cart);
        return save?Result.OK():Result.ERROR();
    }
}
